package com.example.teaknow.ui.Search;

import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SearchView;

import com.example.teaknow.Adapter.NewsAdapter;
import com.example.teaknow.Bean.NewsBean;
import com.example.teaknow.Dao.HistoryDao;
import com.example.teaknow.Dao.NewsDao;
import com.example.teaknow.NewsDetailActivity;
import com.example.teaknow.R;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

public class SearchFragment extends Fragment {
    private SearchView searchView;
    private ListView listView;
    private NewsDao newsDao;
    private HistoryDao historyDao;
    private ArrayList<NewsBean> newsBeans=new ArrayList<>();
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View root = inflater.inflate(R.layout.fragment_search, container, false);
        searchView=(SearchView)root.findViewById(R.id.searchView);
        listView=(ListView)root.findViewById(R.id.lv_load_search);
        searchView.setSubmitButtonEnabled(true);
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            //点击搜索触发
            @Override
            public boolean onQueryTextSubmit(String query) {
                initView(query);
                listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        Intent intent=new Intent(getContext(), NewsDetailActivity.class);
                        NewsBean newsBean=newsBeans.get(position);

                        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// HH:mm:ss
                        Date date = new Date(System.currentTimeMillis());
                        newsBean.setTime(simpleDateFormat.format(date));
                        historyDao=new HistoryDao(getActivity());
                        historyDao.insertHistory(newsBean);

                        intent.putExtra("newsBean",newsBean);
                        startActivity(intent);
                    }
                });
                return true;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                return false;
            }
        });


        return root;
    }

    private void initView(String str) {
        newsDao=new NewsDao(getActivity());
        newsBeans=newsDao.Search(str);
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                listView.setAdapter(new NewsAdapter(newsBeans,getActivity()));
            }
        });


    }
}